#include <algorithm>

#include "Object.h"

using namespace utils;

namespace
{
    struct Destroyer
    {
        template <typename T>
        inline void operator () (T *ptr)
        {
            delete ptr;
        }
    };
}

Object::Object(Object* parent):
parent(parent),
children()
{
    if(parent != NULL)
        parent->addChild(this);
}

Object::~Object()
{
    std::for_each(children.begin(), children.end(), Destroyer());
}

Object* Object::getParent() const
{
    return parent;
}

void Object::setParent(Object* newParent)
{
    if(newParent != parent)
    {
        parent->removeChild(this);
        parent = newParent;
        if(newParent != NULL)
            newParent->addChild(this);
    }
}

void Object::addChild(Object* child)
{
    children.insert(child);
}

void Object::removeChild(Object* child)
{
    children.erase(child);
}
